
Norm.useIdTable = false;

Norm.Database = {
	
	_db : null,
	name : "gears.orm",
	
	getDB : function() {
		
		if (! Norm.Database._db) {
			try {
				Norm.Database._db = google.gears.factory.create('beta.database', '1.0');
				Norm.Database._db.open(Norm.Database.name);
			} catch(e) {
				throw new Error("couldn`t open database: " + Norm.Database.name + " exception: " + e.message);
			}
		}
		
    		return Norm.Database._db;
	},
	
	execute : function(sql, params) {
		
		try {
			if (Norm.debug) {
				Norm.log(sql, params);
			}
			return Norm.Database.getDB().execute(sql, params || []);
		} catch(e) {
			throw new Error(e.message);
		}
	},
	
	executeAndClose : function(sql, params) {
		
		try {
			if (Norm.debug) {
				Norm.log(sql, params);
			}
			var resultSet = Norm.Database.getDB().execute(sql, params || []);
			resultSet.close();
			return resultSet;
		} catch(e) {
			throw new Error(e.message);
		}
	},
	
	close : function() {
		Norm.Database.getDB().close();
	},
	
	extend : function(src, add) {
		
		if (! add) {
			add = src;
			src = this;
		}
		
		for (var property in add) {
			src[property] = add[property];
		}
		
		return src;
	},
	
	getLastInsertRowId : function() {
		return this._db.lastInsertRowId;
	}
	
};
